var app = angular.module('myApp', []);

app.controller('questionAddController', function($rootScope,$scope,$http,$location) {
	var questionTypeList = [
    	{
            "dictionaryCode" : 1,
            "dictionaryValue" : "单选题"
        },
        {
            "dictionaryCode" : 2,
            "dictionaryValue" : "多选题"
        },
        {
            "dictionaryCode" : 3,
            "dictionaryValue" : "判断题"
        },
    ];

    $scope.questionTypeList = questionTypeList;

    $scope.type = $scope.questionTypeList[0];

    $scope.$watch("type", function(n){
        $scope.selectedQuestionTypeID = n.dictionaryCode;
        $scope.dictionaryValue = n.dictionaryValue;
    })

    $scope.search = function() {
    	var findList = [];
    	var id = $scope.searchQustionId;
    	for (var i=0; i<questionList.length; i++) {
    		if (questionList[i].questionId == id) {
    			findList.push(questionList[i]);
    		}
    	}
    	$scope.questionList = findList;
    }

    $scope.questionAddFunc = function() {
        var formdata = {
            questionType : $scope.selectedQuestionTypeID,
            questionContent : $scope.questionContent,
            questionAnswerAnalysis : $scope.answerAnalysis
        };

        if ($scope.selectedQuestionTypeID == 1 ) {
            formdata['selectionA'] = $scope.selectionA;
            formdata['selectionB'] = $scope.selectionB;
            formdata['selectionC'] = $scope.selectionC;
            formdata['selectionD'] = $scope.selectionD;
            formdata['questionAnswer'] = $scope.answerOfSin;
        } else if ($scope.selectedQuestionTypeID == 2) {
            formdata['selectionA'] = $scope.selectionA;
            formdata['selectionB'] = $scope.selectionB;
            formdata['selectionC'] = $scope.selectionC;
            formdata['selectionD'] = $scope.selectionD;
            var tempObj = $scope.answerOfMulti;
            var str = '';
            var indexArr = ['A','B','C','D'];
            for(var key in tempObj) {
                if(tempObj[key]) {
                    if (str.length == 0) {
                        str+=indexArr[key];
                    } else {
                        str+='#'+indexArr[key];
                    }
                }
            }
            formdata['questionAnswer'] = str;
        } else if ($scope.selectedQuestionTypeID == 3) {
            formdata['questionAnswer'] = $scope.answerOfJudge;
        }

        var dataChecked = {};
        for (var key in formdata) {
            if (formdata[key]) {
                dataChecked[key] = formdata[key];
            }
        }

        console.log(dataChecked);

        var url = getBasePath() + 'question/back_addOne';

        $.ajax({
            url : url,
            type : "POST",
            dataType : "json",
            timeout : 10000,
            success : function (data) {
                console.log("success");
                console.log(data);
                alert(data.msg);
                if (data.isSuccess) {
                    location.href = getBasePath() + "admin/question";
                }
            },
            error : function (data) {
                console.log("error");
                console.log(data);
                console.log(typeof data);
            },
            data : formdata
        });
    }

	//$scope.questionAddUrl = getBasePath() + "admin/question/add";

    var getBasePath = globalPath.getBasePath;
});